package com.example.chatty

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.tween
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.IntOffset
import androidx.core.view.WindowCompat
import androidx.navigation.*
import com.example.chatty.logic.model.AppScreen
import com.example.chatty.logic.model.ConversationUiState
import com.example.chatty.logic.utils.LocalNavController
import com.example.chatty.logic.utils.hideIME
import com.example.chatty.screens.conversation.ConversationScreen
import com.example.chatty.screens.conversation.LocalBackPressedDispatcher
import com.example.chatty.screens.conversation.mock.initialMessages
import com.example.chatty.screens.login.Login
import com.example.chatty.ui.Register
import com.example.chatty.ui.components.AppScaffold
import com.example.chatty.ui.theme.ChattyTheme
import com.example.chatty.ui.theme.chattyColors
import com.google.accompanist.navigation.animation.AnimatedNavHost
import com.google.accompanist.navigation.animation.composable
import com.google.accompanist.navigation.animation.rememberAnimatedNavController
import com.google.accompanist.systemuicontroller.rememberSystemUiController

class MainActivity : ComponentActivity() {
    @OptIn(ExperimentalAnimationApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        WindowCompat.setDecorFitsSystemWindows(window, false)
        setContent {
            ChattyTheme {
                val systemUiController = rememberSystemUiController()
                val useDarkIcon = !isSystemInDarkTheme() && MaterialTheme.chattyColors.isLight
                SideEffect {
                    systemUiController.setSystemBarsColor(Color.Transparent, useDarkIcon)
                }

                val navController = rememberAnimatedNavController()

                DisposableEffect(key1 = Unit) {
                    val destinationChangedListener =
                        NavController.OnDestinationChangedListener { controller, destination, arguments ->
                            hideIME()
                        }
                    navController.addOnDestinationChangedListener(destinationChangedListener)
                    onDispose {
                        navController.removeOnDestinationChangedListener(destinationChangedListener)
                    }
                }

                CompositionLocalProvider(
                    LocalNavController provides navController,
                    LocalBackPressedDispatcher provides onBackPressedDispatcher
                ) {
                    ChattyNavHost(navController)
                }
            }
        }
    }
}

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun ChattyNavHost(navController: NavHostController) {
    val transSpec = remember { tween<IntOffset>(400) }

    AnimatedNavHost(navController = navController, startDestination = AppScreen.login) {
        composable(
            route = AppScreen.login,
            enterTransition = null,
            exitTransition = null
        ) {
            Login()
        }
        composable(
            AppScreen.register,
            enterTransition = null,
            exitTransition = null
        ) {
            Register()
        }
        composable(
            AppScreen.main,
            enterTransition = null,
            exitTransition = null,
        ) {
            AppScaffold()
        }
        composable(
            "${AppScreen.conversation}/{uid}",
            arguments = listOf(
                navArgument("uid") {
                    type = NavType.StringType
                }
            ),
            enterTransition = null,
            exitTransition = null
        ) { navBackStackEntry ->
            val uid = navBackStackEntry.arguments?.getString("uid")!!
            ConversationScreen(
                ConversationUiState(
                    initialMessages = initialMessages,
                    conversationUserId = uid
                )
            )
        }
    }
}


@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    ChattyTheme {
        Login()
    }
}